package com.example.module.api;

import java.net.URLEncoder;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import android.content.Context;
import android.util.Log;

public class HttpGetTask extends AsyncHttpTask {

	private static final String TAG = "HttpGetTask";

	public HttpGetTask(Context context) {
		super(context);
	}

	@Override
	public HttpTaskReponse doRequest(APIRequest request) throws Exception {
		
		StringBuffer queryString = new StringBuffer(); 
		if (request.getParams() != null) {
			for (Entry<String, String> entry : request.getParams().entrySet()) {
				if (queryString.length() > 0)
					queryString.append("&");
				else
					queryString.append("?");
				
				queryString.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), HTTP.UTF_8));
			}	
		}
		
		HttpGet httpRequest = new HttpGet(request.getURL() + queryString.toString());
//		httpRequest.setHeader("Content-Type", "application/json");
//		httpRequest.setHeader("SessionKey", mSessionKey);
		
		HttpResponse response = client.execute(httpRequest);

		HttpEntity resEntity = response.getEntity();
		String responseText = EntityUtils.toString(resEntity, HTTP.UTF_8);
				
		Log.d(TAG, "responseText=" + responseText);
		
		return new HttpTaskReponse(RESPONSE_OK, responseText);
	}

}
